package com.artqiyi.dollmachine.views.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseFragment;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.model.entity.DollBean;
import com.artqiyi.dollmachine.model.entity.TipBean;
import com.artqiyi.dollmachine.presenter.DollPresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.CompatUtil;
import com.artqiyi.dollmachine.utils.GlideImageLoader;
import com.artqiyi.dollmachine.utils.GridSpacingItemDecoration;
import com.artqiyi.dollmachine.utils.RxRecyclerViewDividerTool;
import com.artqiyi.dollmachine.utils.ScreenUtil;
import com.artqiyi.dollmachine.utils.manager.ConfigManager;
import com.artqiyi.dollmachine.utils.manager.UserInfoManager;
import com.artqiyi.dollmachine.views.activity.DollOperateActivity;
import com.artqiyi.dollmachine.views.adapter.BulletinViewAdapter;
import com.artqiyi.dollmachine.views.adapter.DollAdapter;
import com.artqiyi.dollmachine.views.widget.BannerLayout;
import com.artqiyi.dollmachine.views.widget.CommonFooter;
import com.artqiyi.dollmachine.views.widget.CommonHeader;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.artqiyi.dollmachine.views.widget.bulletinview.BulletinView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tencent.ilivesdk.ILiveCallBack;
import com.tencent.ilivesdk.core.ILiveLoginManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import timber.log.Timber;

/**
 * 娃娃机列表
 */

public class DollFragment extends BaseFragment<DollPresenter> implements PresenterCallbackCollection.DollFragmentCallBack {
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.xRefreshView)
    XRefreshView mXRefreshView;

    private View mFooterView;

    private DollAdapter mAdapter;

    private List<DollBean> mDollBeans = new ArrayList<>();

    private List<TipBean> mTipDatas = new ArrayList<>();

    private int pageNum = 1;

    private BannerLayout bannerLayout;

    private BulletinView bulletinView;

    @Override
    protected DollPresenter getPresenter() {
        return new DollPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        if (null != ConfigManager.getInstance().getConfigBean()) toolBar.setTitleText(TextUtils.isEmpty(ConfigManager.getInstance().getConfigBean().getApp_name())?"云+物娱":ConfigManager.getInstance().getConfigBean().getApp_name());
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mAdapter = new DollAdapter(mDollBeans);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mRecyclerView.setHasFixedSize(true);
//        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 50, true));
//        mRecyclerView.addItemDecoration(new RxRecyclerViewDividerTool(ScreenUtil.dp2px(getActivity(), 5f)));
        mRecyclerView.setAdapter(mAdapter);
        CommonHeader header = new CommonHeader(getActivity());
        mFooterView = getFooterView();
        mXRefreshView.setCustomHeaderView(header);
        mXRefreshView.setCustomFooterView(new CommonFooter(getActivity()));
        mXRefreshView.setAutoLoadMore(false);
        mXRefreshView.setPinnedTime(1000);
        mXRefreshView.setMoveForHorizontal(true);
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setBackgroundResource(R.color.white);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                mAdapter.removeAllFooterView();
                mXRefreshView.setLoadComplete(false);
                pageNum = 1;
                mPresenter.getDollList(pageNum);
                mPresenter.getBanner();
                mPresenter.getTipList();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                mPresenter.getDollList(pageNum);
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), DollOperateActivity.class).putExtra(Constant.KEY_DOLLBEAN, mDollBeans.get(position)));
            }
        });

        bannerLayout = (BannerLayout) LayoutInflater.from(getActivity()).inflate(R.layout.view_banner, (ViewGroup) mRecyclerView.getParent(), false);
        //设置加载器
        bannerLayout.setImageLoader(new GlideImageLoader());
        mAdapter.setHeaderView(bannerLayout, 0);

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_bulletinview, (ViewGroup) mRecyclerView.getParent(), false);
        bulletinView = (BulletinView) view.findViewById(R.id.bulletin);
        mAdapter.setHeaderView(view, 1);
    }

    @Override
    public void loadData() {
        super.loadData();
        mPresenter.getDollList(pageNum);
        mPresenter.getBanner();
        mPresenter.getTipList();

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    //轮播图
    private void handleBanner(List<String> urls) {
        bannerLayout.setViewUrls(urls);
        //如果只有一张图，取消自动轮播，
        if (null != urls && urls.size() == 1) {
            bannerLayout.setIndicatorContainerInvisible();
            bannerLayout.setAutoPlay(false);
            bannerLayout.setNoScroll(true);
        }
    }

    private View getFooterView() {
        View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.view_footer, (ViewGroup) mRecyclerView.getParent(), false);
        return footerView;
    }

    //请求列表成功
    @Override
    public void onGetDollListSuccess(List<DollBean> dollBeanList) {
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.stopLoadMore();
        }
        if (null != dollBeanList) {
            if (pageNum == 1) {
                mDollBeans.clear();
            }
            mDollBeans.addAll(dollBeanList);

            mAdapter.notifyDataSetChanged();

            if (dollBeanList.size() < 20) {
                mXRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.setFooterView(mFooterView, 0);
                    }
                }, 500);
                mXRefreshView.setLoadComplete(true);
            }
            pageNum++;
        }
    }

    //请求列表失败
    @Override
    public void onGetDollListFail(String msg) {
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.setLoadComplete(true);
        }
        Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
    }

    //获取广告栏成功
    @Override
    public void onGetBannerSuccess(List<String> urls) {
        if (null != urls && urls.size() != 0) {
            handleBanner(urls);
        }
    }

    //获取广告栏失败
    @Override
    public void onGetBannerFail(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    //获取首页娃娃抓中列表成功
    @Override
    public void onTipListSuccess(List<TipBean> beans) {
        if (null != beans) {
            mTipDatas.clear();
            mTipDatas.addAll(beans);
            if (mTipDatas.size() == 0) {
                bulletinView.setVisibility(View.GONE);
                return;
            }
            BulletinViewAdapter adapter = new BulletinViewAdapter(getActivity(), mTipDatas);
            bulletinView.setAdapter(adapter);
        }
    }

    //获取首页娃娃抓中列表失败
    @Override
    public void onTipListFail(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

}
